package me.albert.entitylimiter.utils;

import java.lang.reflect.Method;
import org.bukkit.Bukkit;

public class UtilForFix {
    static Object mcServer;
    static {
        try {
            /*
            Method getCraftServer = Server.class.getMethod("getServer");
            Object craftServer = getCraftServer.invoke(Bukkit.getServer());
            Method getMCServer = craftServer.getClass().getMethod("getServer");
            mcServer = getMCServer.invoke(craftServer);
             */
            String ver = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
            Class<?> mcServerClass = Class.forName("net.minecraft.server." + ver + ".MinecraftServer");
            Method getServerMethod = mcServerClass.getMethod("getServer");
            mcServer = getServerMethod.invoke(null);
            System.out.println(mcServer.getClass());
            mcServer.getClass().getField("recentTps").setAccessible(true);

        } catch (ReflectiveOperationException e) {
            e.printStackTrace();
        }
    }
    public static double[] getTps() {
        if (mcServer == null) {
            return new double[]{0.0, 0.0, 0.0};
        }
        try {
            mcServer.getClass().getField("recentTps").setAccessible(true);
            return (double[]) mcServer.getClass().getDeclaredField("recentTps").get(mcServer);
        } catch (ReflectiveOperationException ignored) {
            return new double[]{0.0, 0.0, 0.0};
        }
    }
}
